package developer.zyk.ability.chat.ability.memory.msgHandler;

import cn.hutool.json.JSONUtil;
import developer.zyk.ability.chat.bo.UserMessageBO;
import developer.zyk.ability.chat.dao.entity.ChatHistoryDetailDO;
import developer.zyk.ability.chat.service.ChatHistoryService;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.stereotype.Component;

/**
 * @author zhangyongkang
 * @time 2025/5/12 16:31
 * @description
 */
@Component("MessageHandlerUser")
public class MessageHandlerUser implements MessageHandler {
    @Resource
    private ChatHistoryService chatHistoryService;

    @Override
    public Message handle(ChatHistoryDetailDO chatHistoryDetailDO) {
        UserMessageBO bo = JSONUtil.toBean(chatHistoryDetailDO.getSendMessage(), UserMessageBO.class);
        return new UserMessage(
                bo.getMessage()
        );
    }

    @Override
    public void add(Message message, Long chatId) {
        chatHistoryService.appendChatDetailWithUser(message.getText(), chatId);
    }
}
